//
//  DINGStatusCell.swift
//  0730Microblog
//
//  Created by ding on 15/8/6.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit

enum DINGStatusCellReusedIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func getCellIdentifier(status: DINGMicroblogStatus) -> String? {
        return (status.retweeted_status == nil) ? DINGStatusCellReusedIdentifier.NormalCell.rawValue : DINGStatusCellReusedIdentifier.ForwardCell.rawValue
    }
}


class DINGStatusCell: UITableViewCell {
    
    var pictureHeightConstraint: NSLayoutConstraint?
    var pictureWidthConstraint: NSLayoutConstraint?
    var pictureTopConstraint: NSLayoutConstraint?
    
    var statuses: DINGMicroblogStatus? {
        didSet {
            topView.statuses = statuses
            pictureView.statuses = statuses
            
            contentLabel.text = statuses?.text
            contentLabel.numberOfLines = 0
            pictureWidthConstraint?.constant = pictureView.bounds.width
            pictureHeightConstraint?.constant = pictureView.bounds.height
            pictureTopConstraint?.constant = (pictureView.bounds.height == 0) ? 0 : 8
            
            contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 20
//            print("-----\(pictureView.bounds.size)------")
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareHomeCellUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareHomeCellUI() {
        contentView.addSubview(separatedView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        separatedView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: separatedView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)) // 居中
        contentView.addConstraint(NSLayoutConstraint(item: separatedView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)) // 距离顶部
        contentView.addConstraint(NSLayoutConstraint(item: separatedView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0)) //宽
        contentView.addConstraint(NSLayoutConstraint(item: separatedView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 10)) // 高
        
        topView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: separatedView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -4))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 66))
        
        contentLabel.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width , multiplier: 1.0, constant: -20))
//        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute , multiplier: 1.0, constant: 200))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
        
        //MARK: - 图片视图的约束
////        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[targetView]-10-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["targetView" : pictureView]))
////        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[topView]-10-[targetView]-10-[bottomView]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["topView": contentLabel, "targetView" : pictureView, "bottomView" : bottomView]))
//        pictureView.translatesAutoresizingMaskIntoConstraints = false
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
//        pictureWidthConstraint = contentView.constraints.last
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: -8))
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 8))
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
//        pictureHeightConstraint = contentView.constraints.last
        
        //MARK: - 底部视图的约束
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: pictureView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8)) // 底部视图top距离图片pictureView
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)) // 底部视图的centerX等于contentView的centerX
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -10)) // 宽
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 44)) // 高度
//         contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -8)) // 底部视图的bottom距离contentView的bottom
        
    }
    
/// 懒加载控件
    private lazy var topView: DINGStatusTopView = DINGStatusTopView()
    
    lazy var contentLabel: UILabel = UILabel(tColor: UIColor.darkGrayColor(), fontSize: 15)
    
    lazy var bottomView: DINGBottomView = DINGBottomView()
    
    private lazy var separatedView: UIView = {
        let sView = UIView()
        sView.backgroundColor = UIColor.lightGrayColor()
        return sView
    }()
   lazy var pictureView: DINGStatusPictureView = DINGStatusPictureView()
    
    //MARK: - 计算行高（最后一个控件的y最大值）
    func getCellHeight(statuses: DINGMicroblogStatus) -> CGFloat {
        self.statuses = statuses
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }

}
